/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.preferences.pages;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipsejcde.core.Activator;
import org.eclipsejcde.core.preferences.JCPreferenceConstants;

/**
 * @author Amgad S Youssef
 *
 */
public class JCWDEPreferences extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {
	private StringFieldEditor portNumberField;

	public JCWDEPreferences() {
		super(GRID);
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
		setDescription("Set Java Card Workstation Development Environment preferences");
	}

	@Override
	public void init(IWorkbench workbench) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void createFieldEditors() {
		portNumberField = new StringFieldEditor(JCPreferenceConstants.JCWDE_CL_OPTIONS_p, "Port number:", getFieldEditorParent());
		addField(portNumberField);

		addField(new BooleanFieldEditor(JCPreferenceConstants.JCWDE_CL_OPTIONS_nobanner, "Suppresses all banner messages", getFieldEditorParent()));
		addField(new BooleanFieldEditor(JCPreferenceConstants.JCWDE_CL_OPTIONS_verbose, "Prints more verbose output", getFieldEditorParent()));
	}

	@Override
	public boolean performOk() {
		if (!validateIsDigits(portNumberField.getStringValue())) {
			setErrorMessage("Please enter a valid port number");
			return false;
		}
		return super.performOk();
	}

	private boolean validateIsDigits(String value) {
		for (int i = 0; i < value.length(); i++) {
			if ("0123456789".indexOf(value.charAt(i)) < 0) {
				return false;
			}
		}
		return true;
	}

}
